/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;

/**
 *
 * @author Administrator
 */
public class MenuCanvas extends GameCanvas implements Runnable, PlayerListener {

    //midlet
    private MMIDlet midlet;
    //------------------------------------------//
    //positions
    private int x = 0;
    private int y = 0;
    private int xPuato = 0;
    private int yPuato = 0;
    //------------------------------------------//
    //image
    private Image imgBackground;
    //------------------------------------------//
    //sprite
    private Sprite spPuato;
    private static int FRAME_PUATO_WIDTH = 0;
    private static int FRAME_PUATO_HEIGHT = 0;
    private boolean running = false;
    private LayerManager lmgr;
    //------------------------------------------//
    //speed
    private int speedGame = 200;
    //------------------------------------------//
    //xy 
    private int xBg = 0;
    private int yBg = 0;
    private int xMenu = 0;
    private int xJarak = 0;
    private int kursorMenu = 0;
    String sMenu[] = new String[7];
    private int statusPet = 0;//
    int arahPet = 0; //kiri
    private int WIDTH = 320;
    private int HEIGHT = 240;
    private String statusKey = "";
    //for sound
    private long delay;
    private Player sound01;

    public MenuCanvas(MMIDlet midlet) {
        super(false);
        setFullScreenMode(true);

        this.midlet = midlet;

        xMenu = (int) (getHeight() * 0.07);

        //jarakLevel = (int) (CANVAS_WIDTH * 0.9);

        //41 x 39
        if (MMIDlet.LANDSCAPE == 1) {
            yBg = - 41;
        } else {
            xBg = - 39;
        }

        sMenu[1] = "Play";
        sMenu[2] = "Highscore";
        sMenu[3] = "Setting";
        sMenu[4] = "Help";
        sMenu[5] = "About";
        sMenu[6] = "Quit";

        kursorMenu = 1;

        FRAME_PUATO_WIDTH = 50;
        FRAME_PUATO_HEIGHT = 50;

        //bgWidth = 640;

        try {
            // image 
            imgBackground = Image.createImage("/set_mainmenubg_all_320x320.png");

            //animated sprite
            spPuato = new Sprite(Image.createImage("/puato.png"),
                    FRAME_PUATO_WIDTH, FRAME_PUATO_HEIGHT);
            xPuato = 10;
            yPuato = 10;
            spPuato.setPosition(xPuato, yPuato);

            //change the reference pixel to the middle of sprite
            //spPuato.defineReferencePixel(FRAME_WIDTH / 2, FRAME_HEIGHT / 2);

            //center the sprite on the canvas
            //(center of sprite is now in center of display)
            //spSpiral.setRefPixelPosition(getWidth() / 2, getHeight() / 2);

            // create and add to layer manager
            lmgr = new LayerManager();
            lmgr.append(spPuato);
            //lmgr.append(spStar);
        } catch (Exception e) {
        }

        if (MMIDlet.LANDSCAPE == 1) {
            xJarak = 26;
        } else {
            xJarak = 30;
            setPortraitDevice();
        }

        try {
            sound01 = Manager.createPlayer(getClass().getResourceAsStream("/menu.mid"), "audio/midi");
            sound01.realize();
            sound01.prefetch();
            sound01.addPlayerListener(this);
            sound01.setLoopCount(-1);
            if (midlet.SOUND == 1) {
                sound01.start();
            } else {
            }
        } catch (Exception e) {
        }
    }

    //start thread
    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    //game loop
    public void run() {
        Graphics g = getGraphics();
        while (running) {
            // look for keypress

            //checkForCollision();
            drawDisplay(g);
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void stop() {
        running = false;
        try {
            sound01.stop();
        } catch (MediaException ex) {
        }
    }

    protected void drawDisplay(Graphics g) {
        g.setColor(0x00ff00);
        ///g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(imgBackground, xBg, yBg, Graphics.TOP | Graphics.LEFT);

        g.setColor(0x0000ff);
        g.drawString(sMenu[1], 50, xMenu + xJarak * 1, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[2], 50, xMenu + xJarak * 2, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[3], 50, xMenu + xJarak * 3, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[4], 50, xMenu + xJarak * 4, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[5], 50, xMenu + xJarak * 5, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[6], 50, xMenu + xJarak * 6, Graphics.TOP | Graphics.LEFT);

        drawHighLight(g, kursorMenu);
        //g.drawString("for test only : " + getWidth() + "x" + getHeight(), 0, 10, Graphics.TOP | Graphics.LEFT);

        //animated sprite, show next frame in sequence

        spPuato.nextFrame();

        if (statusPet == 1) {
            //setWalking();
            petWalking();
        }
        //System.out.println(yPuato);
        spPuato.setPosition(xPuato, yPuato);

        g.drawString(statusKey, 30, 10, Graphics.TOP | Graphics.LEFT);

        //paint all layers
        lmgr.paint(g, 0, 0);
        flushGraphics();
    }

    private void setPortraitDevice() {
        xPuato = HEIGHT - 50;
        yPuato = 50;
        spPuato.setTransform(Sprite.TRANS_ROT90);
    }

    private void setEating() {
        int[] seq = new int[2];
        seq[0] = 3;
        seq[1] = 4;
        spPuato.setFrameSequence(seq);
    }

    private void setWalking() {
        int[] seq = new int[3];
        seq[0] = 11;
        seq[1] = 10;
        seq[2] = 9;
        spPuato.setFrameSequence(seq);
        statusPet = 1;
    }

    private void petWalking() {

        if (MMIDlet.LANDSCAPE == 1) {
            if (arahPet == 1) { //kiri
                if (xPuato > 10) {
                    xPuato = xPuato - 5;
                } else {
                    arahPet = 2;
                }
            } else { //kanan
                spPuato.setTransform(Sprite.TRANS_MIRROR);
                if (xPuato < 100) {
                    xPuato = xPuato + 5;
                } else {
                    spPuato.setTransform(Sprite.TRANS_NONE);
                    arahPet = 1;
                }
            }
        } else {
            //System.out.println("portraite");
            if (arahPet == 1) { //atas-KIRI
                if (yPuato > 10) {
                    yPuato = yPuato - 5;

                } else {
                    arahPet = 2;
                }
            } else { //bawah-KANAN                
                spPuato.setTransform(Sprite.TRANS_MIRROR_ROT90);
                if (yPuato < 100) {
                    yPuato = yPuato + 5;
                    //System.out.println("cek cuy");
                } else {
                    //spPuato.setTransform(Sprite.TRANS_NONE);
                    //System.out.println("arag - 1");
                    spPuato.setTransform(Sprite.TRANS_ROT90);
                    arahPet = 1;
                }
            }
        }

    }

    private void drawHighLight(Graphics g, int i) {
        g.setColor(0xff0000);
        g.drawString(sMenu[i], 50, xMenu + xJarak * i, Graphics.TOP | Graphics.LEFT);
    }

    protected void keyPressed(int keyCode) {

        statusKey = String.valueOf(keyCode);
        //if (MathackMIDlet.LANDSCAPE == 1) { //landscape

        if (keyCode == -5) {
            if (kursorMenu == 1) {
                midlet.showMainGame();
            }
            if (kursorMenu == 6) {
                midlet.exit();
            }
        } else if (keyCode == -2) { //down
            if (kursorMenu == 6) { //down
                kursorMenu = 0;
            }
            kursorMenu++;
            setEating();

        } else if (keyCode == -4) { //right
        } else if (keyCode == -1) { //up
            if (kursorMenu == 1) { //up
                kursorMenu = 7;
            }
            kursorMenu--;
            setWalking();
        } else if (keyCode == -3) { //left
        } else if (keyCode == -7) {
            midlet.exit();
        } else if (keyCode == -6) {
        } else if (keyCode == 50) { //LA-UP 2
        } else if (keyCode == 52) { //LA-LEFT 4
        } else if (keyCode == 53) { //LA-CENTER 5
            if (kursorMenu == 1) {
                midlet.showMainGame();
            }
            if (kursorMenu == 6) {
                midlet.exit();
            }

        } else if (keyCode == 54) { //LA-RIGHT 6
        } else if (keyCode == 56) { //LA-DOWN 8
        } else if (keyCode == 103) { //QW-nofire-CENTER 5
            if (kursorMenu == 1) {
                //midlet.showMainGame();
            }
            if (kursorMenu == 6) {
                midlet.exit();
            }
        }
        repaint();
    }

    public void playerUpdate(Player arg0, String eventType, Object arg2) {
        if (eventType.equals(STARTED)) {
            delay = System.currentTimeMillis() - delay;
            repaint();
        } else if (eventType.equals(END_OF_MEDIA)) {
            delay = System.currentTimeMillis();
        }
    }
}
